from time import sleep

import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin

from tool import common_assert, read_json_data


class TestRegisterLogin:

    def setup_method(self):
        self.session = requests.session()
        log.info("正在获取session对象:{}".format(self.session))
        self.api = ApiRegisterLogin(self.session)
        log.info("正在获取ApiRegisterLogin对象:{}".format(self.api))

    def teardown_method(self):
        self.session.close()
        log.info("正在关闭session对象:{}".format(self.session))

    @pytest.mark.parametrize("random,status_code", read_json_data("register_login.json", "img_code"))
    def test01_img_code(self, random, status_code):
        try:
            result = self.api.api_img_code(random)
            log.info("图片验证码接口测试结果:{}".format(result.status_code))
            common_assert(result, status_code=status_code)
            log.info("图片验证码接口断言通过")
        except Exception as e:
            # 捕获异常的目的是记录错误日志
            log.error(e)
            raise

    @pytest.mark.parametrize("phone,imgVerifyCode,description", read_json_data("register_login.json", "phone_code"))
    def test02_phone_code(self, phone, imgVerifyCode, description):
        try:
            self.api.api_img_code(123123)
            result = self.api.api_phone_code(phone, imgVerifyCode)
            log.info("短信验证码接口测试结果:{}".format(result.text))
            common_assert(result, description=description)
            log.info("短信验证码接口断言通过")
        except Exception as e:
            log.error(e)
            raise

    @pytest.mark.parametrize("phone,password,imgVerifyCode,phone_code,description", read_json_data("register_login.json", "register"))
    def test03_register(self, phone, password, imgVerifyCode, phone_code, description):
        try:
            self.api.api_img_code(123123)
            self.api.api_phone_code(phone, imgVerifyCode)
            result = self.api.api_register(phone, password, imgVerifyCode, phone_code)
            log.info("注册接口测试结果:{}".format(result.text))
            common_assert(result, description=description)
            log.info("注册接口断言通过")
        except Exception as e:
            log.error(e)
            raise

    @pytest.mark.parametrize("keywords,password,description", read_json_data("register_login.json", "login"))
    def test04_login(self, keywords, password, description):
        try:
            i = 1
            result = None
            if password == "error123456":
                while i <= 3:
                    result = self.api.api_login(keywords, password)
                    log.info("登录接口测试结果:{}".format(result.text))
                    i += 1
                assert "锁定" in result.text
                log.info("登录->锁定 断言通过")
                sleep(60)
                result = self.api.api_login(keywords, "y123456")
                log.info("登录接口测试结果:{}".format(result.text))
                common_assert(result, description=description)
                log.info("登录接口断言通过")
            else:
                result = self.api.api_login(keywords, password)
                log.info("登录接口测试结果:{}".format(result.text))
                common_assert(result, description=description)
                log.info("登录接口断言通过")
        except Exception as e:
            log.error(e)
            raise

    @pytest.mark.parametrize("status,description",read_json_data("register_login.json", "login_status"))
    def test05_login_status(self, status, description):
        try:
            if status == "已登录":
                self.api.api_login("15037447712", "y123456")
            result = self.api.api_login_status()
            log.info("登录状态接口测试结果:{}".format(result.text))
            common_assert(result, description=description)
            log.info("登录状态接口断言通过")
        except Exception as e:
            log.error(e)
            raise
